package com.soup.view;

import com.soup.dao.PersonDao;
import com.soup.domain.Person;
import com.soup.utils.StringUtils;
import com.soup.view.component.MassageTip;
import com.soup.view.component.MeFrame;
import com.soup.view.value.ColorValue;
import com.soup.view.value.LoginValue;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Login extends MeFrame implements MouseListener, DocumentListener {

    private JLabel title = new JLabel("图书管理系统");

    private JTextField acc = new JTextField(10);
    private JPasswordField pass = new JPasswordField(10);
    private JRadioButton isStudent = new JRadioButton("学生");
    private JRadioButton isRoot = new JRadioButton("管理员");
    private JButton login = new JButton("登录");

    private JLabel reg = new JLabel("没有账号，去注册？");

    private PersonDao dao = new PersonDao();


    private MeFrame operation = null;

    public Login() {
        super("登录",600, 450);

        title.setBounds(30,40,350,50);
        context.add(title,28);
        title.setForeground(Color.RED);

        acc.setBounds(120,130,350,48);
        context.add(acc,"账户",100);
        acc.getDocument().addDocumentListener(this);

        pass.setEchoChar('*');
        pass.setBounds(120,180,350,48);
        context.add(pass,"密码",100);

        ButtonGroup group = new ButtonGroup();
        group.add(isRoot);
        group.add(isStudent);

        isStudent.setBounds(120,240,100,48);
        context.add(isStudent,20);
        isRoot.setBounds(240,240,100,48);
        context.add(isRoot,20);
        isStudent.setSelected(true);

        login.setBounds(120,300,350,42);
        context.add(login,20);

        reg.setBounds(120,350,300,48);
        context.add(reg,14);
        reg.setForeground(Color.LIGHT_GRAY);

        login.addActionListener(this);
        reg.addMouseListener(this);
    }

    @Override
    public void doActionPerformed(ActionEvent e) {
        if (e.getSource() == login){

            if (!StringUtils.isEmpty(acc.getText())){
                if (!StringUtils.isEmpty(new String(pass.getPassword()))){
                    Map<String,Object> map = new HashMap<String,Object>();
                    if (acc.getText().matches("[0-9]+") && acc.getText().length() == 9){
                        map.put("id",acc.getText());
                    }else{
                        map.put("name",acc.getText());
                    }
                    map.put("password",new String(pass.getPassword()));
                    if (isStudent.isSelected()){
                        map.put("ind","学生");
                    }else{
                        map.put("ind","管理员");
                    }
                    List<Person> query = dao.query(map);
                    System.out.println(query);
                    if (query.size() > 0){
                        if (isStudent.isSelected()){
                            operation = new StudentFrame(this,query.get(0));
                            this.setVisible(false);
                            operation.setVisible(true);
                        }else{
                            operation = new RootFrame(this,query.get(0));
                            this.setVisible(false);
                            operation.setVisible(true);
                        }
                        LoginValue.name = query.get(0).getName();
                        LoginValue.id = query.get(0).getId();
                    }else {
                        MassageTip.showMassage(this,"账户或密码错误");
                    }
                }else{
                    MassageTip.showMassage(this,"密码不能为空");
                }
            }else {
                MassageTip.showMassage(this,"账户不能为空");
            }
        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == reg){
            new Register().setVisible(true);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getSource() == reg){
            reg.setForeground(Color.RED);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        if (e.getSource() == reg){
            reg.setForeground(Color.LIGHT_GRAY);
        }
    }

    public void check(DocumentEvent e){
        if (e.getDocument() == acc.getDocument()) {
            if (acc.getText().matches("[0-9]+")) {
                if (acc.getText().length() != 9) {
                    acc.setForeground(Color.RED);
                }else {
                    acc.setForeground(ColorValue.ContextForeground);
                }
            }else {
                if (acc.getText().length() >= 2 && acc.getText().length() < 10) {
                    acc.setForeground(ColorValue.ContextForeground);
                }else {
                    acc.setForeground(Color.RED);
                }
            }
        }
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        check(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        check(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {

    }
}
